
var config = $.extend(true, {}, listApp, {
    data : {
        service : deviceService,
        treeData:{},
        searchVm:{
            name:'',
            deviceCode:'',
            protocolType:'',
            netName:'',
            protocolName:'',
            spotIds:[]
        },
        tableVm:{
            loading:false
        },
        editVm:{
            // data:{
            //     type:'snmp'
            // },
            // chkTimeout:true,
            editFormRules:{
                formRules:{
                    name : [ {required : true,message : '请输入站点名称',trigger : 'change'} ],
                    flag:[{
                        validator:function(rule, value, callback){
                            if(value==undefined || value.length==0){
                                callback();
                            }
                            var reg = /^[A-Za-z0-9]+$/;
                            if(reg.test(value)){
                                callback();
                            }else{
                                callback(new Error('请输入数字和字母'));
                            }
                        },
                        trigger:'blur'
                    }],
                    spotId : [ {required : true,message : '请输入所属站点',trigger : 'change'} ],
                    deviceCode:[ {required : true,message : '请输入通信识别码',trigger : 'change'} ],
                    type:[{required : true,message : '请选择类型',trigger : 'change'}],
                    protocolId:[{required : true,message : '请选择协议',trigger : 'change'}],
                    sendType:[{required : true,message : '请选择上传方式',trigger : 'change'}],
                    timeout:[{required : true,message : '请填写通信报警时间',trigger : 'change'},
                        {
                            validator:function(rule, value, callback){
                                var regPos = /^\d+(\.\d+)?$/; //非负浮点数
                                var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
                                if(regPos.test(value) || regNeg.test(value)){
                                    callback();
                                }else{
                                    callback(new Error('请输入数值'));
                                }
                            },
                            trigger:'blur'
                        }
                    ]
                }
            }
        },
        treeStyle:{
            float:'left',
            width:'20%'
        },
        tableStyle:{
            float:'left',
            marginLeft:'20px',
            width:'78%'
        },
        protocolList:[],
        sendTypeList:[{
            id:1,
            name:"仅输出被选通道数据到服务器"
        },{
            id:0,
            name:"输出所有通道数据到服务器"
        }],
        spotList:[],
        timeoutInfo:false,
        requestPagePath:'requestPageList',
        upDataList:[{
            id:1,
            name:"是"
        },{
            id:0,
            name:"否"
        }],
        addVm:{
            channelTimeout:true,
            sendtrap:1,
            timeout:5
        },
        treeChildren:[]
    },
    methods : {
        init:function () {
            var _this=this;
            _this.loadSpotList();
            //加载mib库列表(根据类型查找)
            var param={
                type:this.editVm.data.type == undefined ? "" :(this.editVm.data.type == "虚拟设备" ? "": this.editVm.data.type)
            }
            _this.loadProtocolList(param);

        },
        loadSpotList:function(){
            var _this=this;
            //加载spotList列表
            spotService.getAllSpot("/getAllSpot",null,{
                success : function(ret) {
                    _this.spotList=ret;

                    if(_this.treeData!=undefined && _this.treeData.id!=undefined &&
                        !_this.treeData.isDevice && _this.editVm.isAdd && _this.editVm.visible){
                        _this.$set(_this.editVm.data,"spotId",_this.treeData.id);
                    }

                },
                fail : function(res) {
                    _this.showFailMsg(res.msg);
                }
            })
        },
        changType:function(){
            var param={
                type:this.editVm.data.type == undefined ? "" :(this.editVm.data.type == "虚拟设备" ? "": this.editVm.data.type)
            }
            this.loadProtocolList(param);
            //去除protocolId 验证
            if(this.$refs['deal']!=null && this.$refs['deal']!=undefined)
                this.$refs['deal'].resetField();
            if(this.$refs['mib']!=null && this.$refs['mib']!=undefined)
                this.$refs['mib'].resetField();

            this.$set(this.editVm.data, 'protocolId', '');
            this.$set(this.editVm.data, 'sendtrap', 1);
        },
        loadProtocolList:function(param){
            var _this=this;
            protocolService.getListByType("/getProtocolListByType",param,{
                success:function (ret) {
                    _this.protocolList=ret;
                },
                fail:function (res) {
                    _this.showFailMsg(res.msg);
                }
            })
        },
        loadList:function(){
            this.init();
            if(this.$refs['editForm']!=null && this.$refs['editForm']!=undefined)
                this.$refs['editForm'].resetFields();


        },
        getTreeSelectedVal:function (data) {
            this.treeData = data;
            this.searchVm.spotIds=this.treeChildren;
            this.searchVm.spotIds.push(data.id);
            this.search();
        },
        getTreeChildren:function(data){
            var _this=this;
            _this.treeChildren=[];
            if(data!=null && data!=undefined && data.length>0){
                data.forEach(function(item,index,array){
                    //执行代码
                    _this.treeChildren.push(item.id);
                })
                console.log(_this.treeChildren)
            }
        },
        collapse:function (isCollapsed) {

            if(isCollapsed){
                this.treeStyle.width='3%';
                this.tableStyle.width='80%';
            }else{
                this.treeStyle.width='20%';
                this.tableStyle.width='78%';
            }
        },
        editDevice:function (row) {
            // console.log('this.editVm.data',this.editVm.data)

        },
        buildSaveObj:function (vm) {
            //构造保存实体
            this.$set(vm.data, 'virtualType', (vm.data.type!='虚拟设备' ? 0:1));
            this.$set(vm.data, 'channelTimeout', (vm.data.channelTimeout ? 1: 0));

        },
        fillData:function(row){
            this.loadSpotList();

            var param={
                type:row.protocolType
            }
          this.loadProtocolList(param);
          if(row.virtualType==1) {
              this.$set(this.editVm.data, 'type', "虚拟设备");
          }else{
              this.$set(this.editVm.data, 'type', row.protocolType);
          }

          if(row.channelTimeout ==1){
              this.$set(this.editVm.data, 'channelTimeout', true);
          }else{
              this.$set(this.editVm.data, 'channelTimeout', false);
          }
        },
        // changeTimeOut:function (prop,val) {
        //     var _this=this;
        //     var arr=prop.split(",");
        //     if(val){
        //         if(arr.length>0){
        //             arr.forEach(function (value) {
        //                 _this.$refs['editForm'].validateField(value);
        //             })
        //         }
        //     }else{
        //         if(arr.length>0){
        //             arr.forEach(function (value) {
        //                 _this.$refs[value].resetField();
        //             })
        //
        //         }
        //     }
        // },
        formatter:function (row) {
            return row.virtualType==1 ? '虚拟设备': row.protocolType;
        },
        save : function(vm,callback,beforeSave) {
            var _this = this;
            this.$refs[vm.formName].validate(function(valid) {
                if (valid) {
                    vm.loading = true;
                    _this.service.save(_this.buildSaveReqDto(vm,beforeSave), _this.savePath || 'save', {
                        success : function(ret) {
                            vm.loading = false;
                                _this.requsetPage();
                                vm.visible = false;
                            _this.saveback();
                        },
                        fail : function(res) {
                            vm.loading = false;
                            _this.showFailMsg(res.msg);
                            if(_this.editVm.data.channelTimeout ==1){
                                _this.$set(_this.editVm.data, 'channelTimeout', true);
                            }else{
                                _this.$set(_this.editVm.data, 'channelTimeout', false);
                            }
                        }
                    });
                } else {
                    _this.showFailMsg('校验不通过哦');
                    return false;
                }
            });
        }

    }
});
var app = new Vue(config);